package net.hn.hnms.biz.safety.monitor.group.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import net.hn.hnms.biz.api.safety.gasext.domain.TransfiniteStatisticsVo;
import net.hn.hnms.biz.api.safety.gasext.fegin.RemoteDwsService;
import net.hn.hnms.biz.api.safety.location.model.GroupAlarmDataTotal;
import net.hn.hnms.biz.api.safety.monitor.model.MonitorTodayInfoTotal;
import net.hn.hnms.biz.safety.monitor.domain.vo.NowSensorAlarmVo;
import net.hn.hnms.biz.safety.monitor.group.entity.*;
import net.hn.hnms.biz.safety.monitor.group.entity.dto.MineDataResponse;
import net.hn.hnms.biz.safety.monitor.group.entity.dto.OtherApiAqjkNowDto;
import net.hn.hnms.biz.safety.monitor.group.service.GroupMonitorService;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.redis.utils.RedisUtils;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 智呈物联 pan'sx
 * 2022/12/20 15:59
 * 1.0 version
 */
@RestController
@RequestMapping("group/riskMonitor/monitor")
@Api(value = "安全监控弹出列表", tags = {"安全监控-集团端"})
@RequiredArgsConstructor
public class GroupMonitorController {
    private final GroupMonitorService groupMonitorService;

    @Resource
    private RemoteDwsService remoteDwsService;

    @GetMapping("riskSituation")
    @ApiOperation("风险状态")
    public R<MonitorTodayInfoTotal> riskSituation(String companyCode){
        return R.ok(groupMonitorService.riskSituation(companyCode));
    }
    @GetMapping("nowAlarm")
    @ApiOperation("报警信息")
    public R<GroupAlarmDataTotal> alarm(String companyCode){
        return R.ok(groupMonitorService.alarm(companyCode));
    }
    @GetMapping("/getTodayNowSensorAlarmByGasesType")
    @ApiOperation("根据气体获取当天实时传感器报警")
    @AutoSetMineCode
    public R<List<NowSensorAlarmVo>> getTodayNowSensorAlarmByGases(@ApiParam(value = "气体:环境瓦斯,一氧化碳,风速，粉尘等",required = true,defaultValue = "环境瓦斯") String gasesTypeName){
        List<NowSensorAlarmVo>  list= groupMonitorService.getTodayNowSensorAlarmByGases(gasesTypeName);
        return R.ok(list);
    }

    /**
     * 获取大屏人员监测信息
     * @return R
     */
    @GetMapping("getSafetyMonitoring")
    @ApiOperation("大屏首页-安全检测")
    public R<SafetyMonitoringVo> getSafetyMonitoring() {
        SafetyMonitoringVo safetyMonitoring = groupMonitorService.getSafetyMonitoring();
        return R.ok(safetyMonitoring);
    }

    /**
     * 获取大屏人员监测信息（本地获取）
     * @return R
     */
    @GetMapping("getSafetyMonitoringLocl")
    @ApiOperation("大屏首页-安全检测（本地数据）")
    public R<SafetyMonitoringVo> getSafetyMonitoringLocl(@ApiParam(value = "公司编码") String companyCode,
                                                         @ApiParam(value = "煤矿编码") String mineCode) {
        SafetyMonitoringVo safetyMonitoring = new SafetyMonitoringVo();
        Boolean istru = RedisUtils.getCacheObject(Constants.SYS_DWS_KEY);
        if (istru == null || !istru) {
            safetyMonitoring = groupMonitorService.getSafetyMonitoringLocl();
        } else {
            net.hn.hnms.biz.api.safety.gasext.domain.SafetyMonitoringVo data = remoteDwsService.getSafetyMonitoringLocl(companyCode,mineCode).getData();
            BeanUtils.copyProperties(data,safetyMonitoring);
            List<MinePointInAlarmfoVo> collect = data.getMinePointInAlarmfoList().stream().map(t -> {
                MinePointInAlarmfoVo minePointInAlarmfoVo = new MinePointInAlarmfoVo();
                minePointInAlarmfoVo.setCoWarnCount(t.getCoWarnCount());
                minePointInAlarmfoVo.setCH4WarnCount(t.getCH4WarnCount());
                minePointInAlarmfoVo.setMineCode(t.getMineCode());
                minePointInAlarmfoVo.setMineName(t.getMineName());
                minePointInAlarmfoVo.setPointNumber(t.getPointNumber());
                minePointInAlarmfoVo.setRemark(t.getRemark());
                return minePointInAlarmfoVo;
            }).collect(Collectors.toList());
            safetyMonitoring.setMinePointInAlarmfoList(collect);
        }
        return R.ok(safetyMonitoring);
    }

    /**
     * 获取龙软（井工矿）报警数据（甲烷和一氧化碳报警）入库
     * @return R
     */
    @GetMapping("getLongRuanAlarmList")
    @ApiOperation("获取龙软（井工矿）报警数据（甲烷和一氧化碳报警）入库")
    public MineDataResponse<List<OtherApiAqjkNowDto>>  getLongRuanAlarmList() {
        MineDataResponse<List<OtherApiAqjkNowDto>> otherSystemGroupAqjkNowApi = groupMonitorService.getLongRuanAlarmList();
        return otherSystemGroupAqjkNowApi;
    }

    /**
     * 超员情况
     * @return R
     */
    @GetMapping("getTransfiniteStatistics")
    @ApiOperation("大屏首页-超限统计")
    public R<TransfiniteStatisticsVo> getTransfiniteStatistics(@ApiParam(value = "公司编码") String companyCode,
                                                               @ApiParam(value = "煤矿编码") String mineCode,@ApiParam(value = "查询类型：周或月，不传默认十天") String type) {
        TransfiniteStatisticsVo data = remoteDwsService.getTransfiniteStatistics(companyCode,mineCode,type).getData();
//        TransfiniteStatisticsVo transfiniteStatisticsVo = groupMonitorService.getTransfiniteStatistics(type);
        return R.ok(data);
    }

    /**
     * 人员监测联网情况
     * @return R
     */
    @GetMapping("getOnLineMineInfo")
    @ApiOperation("大屏首页-安全监测联网情况")
    public R<OnLineMineInfoVo> getOnLineMineInfo() {
        OnLineMineInfoVo OnLineMineInfoVo = groupMonitorService.getOnLineMineInfo();
        return R.ok(OnLineMineInfoVo);
    }

    /**
     * 大屏首页-主通风详情
     * @return R
     */
    @GetMapping("getFanSystemInfoList")
    @ApiOperation("大屏首页-安全监测主通风详情")
    public R<List<FanSystemVolumeAndSpeedVo>> getFanSystemInfoList() {
        List<FanSystemVolumeAndSpeedVo> fanSystemVolumeAndSpeedVos = groupMonitorService.getFanSystemInfoList();
        return R.ok(fanSystemVolumeAndSpeedVos);
    }
    @GetMapping("getMineHazardTypeHydrologyList")
    @ApiOperation("大屏首页-煤矿地质-水害地质类型列表")
    public R<GeologyMineListVO> getMineHazardTypeHydrologyList(@ApiParam(value = "公司编码") String companyCode,
                                                               @ApiParam(value = "煤矿编码") String mineCode) {
        GeologyMineListVO geologyMineListVO = groupMonitorService.getMineHazardTypeHydrologyList(companyCode,mineCode);
        return R.ok(geologyMineListVO);
    }

    @GetMapping("getMineGassyMineList")
    @ApiOperation("大屏首页-煤矿地质-瓦斯等级列表")
    public R<GassyMineListVO> getMineGassyMineList(@ApiParam(value = "公司编码") String companyCode,
                                                   @ApiParam(value = "煤矿编码") String mineCode) {
        GassyMineListVO gassyMineListVO = groupMonitorService.getMineGassyMineList(companyCode,mineCode);
        return R.ok(gassyMineListVO);
    }
    @GetMapping("getMineImpactMineList")
    @ApiOperation("大屏首页-煤矿地质-冲击地压危险等级列表")
    public R<ImpactMineListVO> getMineImpactMineList(@ApiParam(value = "公司编码") String companyCode,
                                                     @ApiParam(value = "煤矿编码") String mineCode) {
        ImpactMineListVO impactMineListVO = groupMonitorService.getMineImpactMineList(companyCode,mineCode);
        return R.ok(impactMineListVO);
    }


    @GetMapping("dws")
    @ApiOperation("切换dws数据")
    public R dws() {
        Boolean istru = RedisUtils.getCacheObject(Constants.SYS_DWS_KEY);
        String dws = "dwsz状态：";
        if (istru == null) {
            istru = true;
            RedisUtils.setCacheObject(Constants.SYS_DWS_KEY, true);
            return R.ok(dws + istru);
        }
        if (istru) {
            RedisUtils.setCacheObject(Constants.SYS_DWS_KEY, false);
            return R.ok(dws + false);
        } else {
            RedisUtils.setCacheObject(Constants.SYS_DWS_KEY, true);
            return R.ok(dws + true);
        }

    }
    @GetMapping("dwsStatus")
    @ApiOperation("获取Dws状态")
    public R dwsStatus() {
        Boolean istru = RedisUtils.getCacheObject(Constants.SYS_DWS_KEY);
        String dws = "dwsz状态：";
        if (istru == null || !istru) {
            return R.ok(dws + istru, false);
        } else {
            return R.ok(dws + istru, true);
        }

    }
}
